package com.glp.work.flow.service.impl;

import com.glp.fbs.api.dto.user.PersonInfoDto;
import com.glp.work.flow.mapper.WfUserBusinessRangeMapper;
import com.glp.work.flow.pojo.FlowTaskDto;
import com.glp.work.flow.pojo.WfUserBusinessRange;
import com.glp.work.flow.service.client.PermissionClient;
import com.glp.work.flow.service.UserService;
import com.glp.work.flow.service.WfUserBusinessRangeService;
import com.glp.work.flow.service.WfUserSuperiorService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : 郭永伟
 * @date : 2020/10/20/13:21
 * @description : 用户业务范围
 */
@Service
public class WfUserBusinessRangeServiceImpl implements WfUserBusinessRangeService {
    @Resource
    private WfUserBusinessRangeMapper wfUserBusinessRangeMapper;
    @Resource
    private PermissionClient permissionClient;
    @Resource
    private WfUserSuperiorService wfUserSuperiorService;
    @Resource
    private UserService userService;

    @Override
    public void updateOnUserNo(FlowTaskDto flowTaskDto, PersonInfoDto personInfo) {
        Example example = new Example(WfUserBusinessRange.class);
        example.createCriteria().andEqualTo("appId", flowTaskDto.getAppId())
                .andEqualTo("processId",flowTaskDto.getProcInsId())
                .andEqualTo("onUserNo", flowTaskDto.getAssignee());
        WfUserBusinessRange wfUserBusinessRange = wfUserBusinessRangeMapper.selectOneByExample(example);
        if (personInfo != null) {
            List<String> businessRangeCodes = personInfo.getBusinessRangeCodes();
            if (wfUserBusinessRange == null) {
                wfUserBusinessRange = new WfUserBusinessRange();
                wfUserBusinessRange.setAppId(flowTaskDto.getAppId());
                wfUserBusinessRange.setProcessId(flowTaskDto.getProcInsId());
                wfUserBusinessRange.setOnUserNo(flowTaskDto.getAssignee());
                String businessRangeCode = businessRangeCodes.toString();
                String substring = businessRangeCode.substring(1, businessRangeCode.length() - 1);
                wfUserBusinessRange.setOnBusinessScopeCode(substring);
                wfUserBusinessRangeMapper.insertSelective(wfUserBusinessRange);
            } else {
                String businessRangeCode = businessRangeCodes.toString();
                String substring = businessRangeCode.substring(1, businessRangeCode.length() - 1);
                wfUserBusinessRange.setOnBusinessScopeCode(substring);
                wfUserBusinessRangeMapper.updateByExampleSelective(wfUserBusinessRange, example);
            }
            //根据processId修改业务范围 CRM用作动态分配提交人业务范围
            if (flowTaskDto.getDynamicBusinessScope() != null
                    && businessRangeCodes.toString().contains(flowTaskDto.getDynamicBusinessScope())) {
                Example example1 = new Example(WfUserBusinessRange.class);
                example1.createCriteria().andEqualTo("processId", flowTaskDto.getProcInsId());
                wfUserBusinessRange = new WfUserBusinessRange();
                wfUserBusinessRange.setAppId(flowTaskDto.getAppId());
                wfUserBusinessRange.setProcessId(flowTaskDto.getProcInsId());
                wfUserBusinessRange.setBusinessScopeCode(flowTaskDto.getDynamicBusinessScope());
                wfUserBusinessRangeMapper.updateByExampleSelective(wfUserBusinessRange, example1);
            }
        }

    }

    @Override
    public void insertUserNo(WfUserBusinessRange wfUserBusinessRange) {
        Example example = new Example(WfUserBusinessRange.class);
        example.createCriteria().andEqualTo("appId", wfUserBusinessRange.getAppId())
                .andEqualTo("processId", wfUserBusinessRange.getProcessId());
        WfUserBusinessRange wfUserBusinessRange1 = wfUserBusinessRangeMapper.selectOneByExample(example);
        if (wfUserBusinessRange1 == null) {
            wfUserBusinessRange1 = new WfUserBusinessRange();
            wfUserBusinessRange1.setAppId(wfUserBusinessRange.getAppId());
            wfUserBusinessRange1.setProcessId(wfUserBusinessRange.getProcessId());
            wfUserBusinessRange1.setBusinessScopeCode(wfUserBusinessRange.getBusinessScopeCode());
            wfUserBusinessRangeMapper.insertSelective(wfUserBusinessRange1);
        } else {
            wfUserBusinessRange1.setBusinessScopeCode(wfUserBusinessRange.getBusinessScopeCode());
            wfUserBusinessRangeMapper.updateByExampleSelective(wfUserBusinessRange1, example);
        }
    }

    @Override
    public void updateBusinessScopeCode(WfUserBusinessRange wfUserBusinessRange) {
        Example example = new Example(WfUserBusinessRange.class);
        example.createCriteria().andEqualTo("appId", wfUserBusinessRange.getAppId())
                .andEqualTo("processId", wfUserBusinessRange.getProcessId());
        WfUserBusinessRange wfUserBusinessRange1 = wfUserBusinessRangeMapper.selectOneByExample(example);
        if (wfUserBusinessRange1 != null) {
            wfUserBusinessRange1.setBusinessScopeCode(wfUserBusinessRange.getBusinessScopeCode());
            wfUserBusinessRangeMapper.updateByExampleSelective(wfUserBusinessRange1, example);
        }
    }
}
